﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using EzDesk.DevHelper.DbMgr.Views;

namespace EzDesk.DevHelper.DbMgr.Presenters
{
    public class DbMgrPresenterBase
    {
        protected static ILogView _logView;

        protected static string _dataSourceName;


        protected readonly List<string> _databasePrjNamelist = new List<string>()
                                                {
                                                    "EzDesk.Database.EzDeskAspnetDatabase",
                                                    "EzDesk.Database.EzDeskAttendance",
                                                    "EzDesk.Database.EzDeskDatabase",
                                                    "EzDesk.Database.EzDeskMessage",
                                                    "EzDesk.Database.EzDeskProject",
                                                    "EzDesk.Database.EzDeskStaff",
                                                };

        protected readonly List<string> _databaseNameList = new List<string>()
                                                              {
                                                                  "EzDeskAspnetDatabase",
                                                                  "EzDeskAttendance",
                                                                  "EzDeskDatabase",
                                                                  "EzDeskMessage",
                                                                  "EzDeskProject",
                                                                  "EzDeskStaff",
                                                                  "",
                                                              };

        protected string getConnectionString(string databaseName)
        {
            return string.Format(@"Data Source={0};Initial Catalog={1};Integrated Security=True",
                               _dataSourceName, databaseName);
        }

        protected string getEntityConnectionString(string databaseName)
        {
            return string.Format(@"metadata=res://*/EzDeskDatabase.csdl|res://*/EzDeskDatabase.ssdl|res://*/EzDeskDatabase.msl;provider=System.Data.SqlClient;provider connection string='Data Source={0};initial catalog={1};integrated security=True;multipleactiveresultsets=True;App=EntityFramework'",
                   _dataSourceName, databaseName);
        }

        /// <summary>
        ///  将某个文件copy到开发环境下
        /// </summary>
        public void CopyFileToDevEnv(string fileName)
        {
            // 看看是不是开发环境
            bool isDevEnv = false;
            string x = AppDomain.CurrentDomain.BaseDirectory;
            var di =new DirectoryInfo(x);
            if (di.Parent != null)
            {
                isDevEnv = di.Name.ToLower() == "debug" && di.Parent.Name.ToLower() == "bin";
            }

            if (isDevEnv)
            {
                // 是的话尝试copy
                string oriFileName = Path.Combine(di.FullName, fileName);
                string targetFileName = Path.Combine(di.Parent.Parent.FullName, fileName);
                File.Copy(oriFileName, targetFileName, true);
                _logView.WriteLine("更新开发文件： " + fileName + " 成功");
            }
            
        }

        protected List<string> getAvailableSchemaList()
        {
            var schemaList = new List<string>();

            var manifestExt = ".deploymanifest";
            foreach (var databaseName in _databasePrjNamelist)
            {
                var targetName = databaseName + manifestExt;
                if (File.Exists(targetName))
                {
                    schemaList.Add(targetName);
                }
            }
            return schemaList;
        }

        protected void refreshDatabaseSchema(List<string> dataSchemaList)
        {
            string updateSchemaCmdText =
                "VSDBCMD /a:Deploy /dd:+ /manifest:{0} /cs:\"Data source={1};Integrated Security=true\" /p:BlockIncrementalDeploymentIfDataLoss=false";

            foreach (var schemaName in dataSchemaList)
            {
                if (File.Exists(schemaName))
                {
                    var cmdText = string.Format(
                        updateSchemaCmdText,
                        schemaName,
                        _dataSourceName);

                    _logView.WriteLine("执行命令： " + cmdText);

                    Thread.Sleep(0);
                    // 对Schema进行操作
                    string log = Utility.ExecuteCmd(cmdText);
                    _logView.WriteLine(log);
                }
                else
                {
                    _logView.WriteLine("没找到要更新的Schema: " + schemaName);
                }
            }

            _logView.WriteLine("更新完成");
        }

        protected void refreshAllAvailableDatabaseSchema()
        {
            refreshDatabaseSchema(getAvailableSchemaList());
        }
    }
}
